//
//  RegisterViewController.m
//  WuJieLive
//
//  Created by iMac on 2017/10/18.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "RegisterViewController.h"
#import "VerificationCodeView.h"
#import "MessageRegisterViewController.h"

@interface RegisterViewController ()<NextStepDelegate>
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UITextField *phoneText;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)setNavi
{
    self.title = @"注册";
    
}

- (void)setUI
{
    [self.phoneText setValue:TextInputColor forKeyPath:@"_placeholderLabel.textColor"];
    [self.phoneText setValue:FontSize(15) forKeyPath:@"_placeholderLabel.font"];
    
    [self.phoneText addTarget:self action:@selector(changeLength:) forControlEvents:(UIControlEventEditingChanged)];

}

- (void)changeLength:(UITextField *)textField
{
    if (self.phoneText.text.length == 11) {
        self.nextBtn.userInteractionEnabled = YES;
        [self.nextBtn setBackgroundColor:MainColor];
    }else
    {
        [self.nextBtn setBackgroundColor:MainBtnColor];
        self.nextBtn.userInteractionEnabled = NO;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark 下一步
- (IBAction)next:(id)sender {
    [self.view endEditing:YES];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"phone_mob":self.phoneText.text};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/register-phone",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            self.redisKey = responseObject [@"data"][@"phone_redis_key"];
            NSDictionary * tempDict= @{@"phone_redis_key":self.redisKey};
            [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/register-images-code",BaseUrl] para:tempDict success:^(BOOL isSuccess, id responseObject) {
                if (isSuccess) {
                    self.telephoneNumber = self.phoneText.text;
                    VerificationCodeView * view = [[VerificationCodeView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
                    [view.codeImageView sd_setImageWithURL:responseObject[@"data"][@"captcha_path"]];
                    view.delegate = self;
                    [self.view addSubview:view];
                }
            } failure:^(NSError *error) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            }];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (void)GetCode:(NSString *)code
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"phone_redis_key":self.redisKey,@"image_code":code};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/register-images-code-check",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            NSDictionary * dic = @{@"phone_redis_key":self.redisKey};
            [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/send-register-sms",BaseUrl] para:dic success:^(BOOL isSuccess, id responseObject) {
                if (isSuccess) {
                    MessageRegisterViewController * vc = [[MessageRegisterViewController alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            } failure:^(NSError *error) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            }];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
